<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

use  App\Models\Weixin;

use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;

class WeixinController extends Controller
{
    public function create(Request $request)
    {
        $input = Input::only('userName','alias','nickName','comRemark','sex','provincialPy','cityPy','signature','smallHeadUrl','bigHeadUrl');
        $validator = Validator::make($input, [        
            'userName' => 'required',
            'alias' => 'nullable',
            'nickName' => 'nullable',
            'comRemark' => 'nullable',
            'sex' => 'nullable',
            'provincialPy' => 'nullable',
            'cityPy' => 'nullable',
            'signature' => 'nullable',
            'smallHeadUrl' => 'nullable',
            'bigHeadUrl' => 'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $where['uid'] = $input['userName'];
        $exist = Weixin::where($where)->first();
        if($exist){
            $exist->alias = $input['alias'];
            $exist->nickName = $input['nickName']??'';
            $exist->com_remark = $input['comRemark']??'';
            $exist->sex = $input['sex'];
            $exist->province = $input['provincialPy']??'';
            $exist->city = $input['cityPy']??'';
            $exist->signature = $input['signature']??'';
            $exist->small_head_url = $input['smallHeadUrl']??'';
            $exist->big_head_url = $input['bigHeadUrl']??'';
            $exist->save();
        }else{
            $data['uid'] = $input['userName'];
            $data['alias'] = $input['alias']??'';
            $data['nickname'] = $input['nickName']??'';
            $data['com_remark'] = $input['comRemark']??'';
            $data['sex'] = $input['sex'];
            $data['province'] = $input['provincialPy']??'';
            $data['city'] = $input['cityPy']??'';
            $data['signature'] = $input['signature']??'';
            $data['small_head_url'] = $input['smallHeadUrl']??'';
            $data['big_head_url'] = $input['bigHeadUrl']??'';
            $data['phone_client_id'] = $input['phone_client_id']??'';
            Weixin::create($data);
        }
        return $this->returnOk();
    }
}

